﻿using System;
using System.Windows.Media;

namespace WickedFlame.Core.Extensions
{
    public static class ColorExtensions
    {
        //public static Color FromString(this Color color, string value)
        //{
        //    if(value.Length < 8)
        //        return Color.FromArgb(Convert.ToByte("FF"), GetByte(value, 1), GetByte(value, 3), GetByte(value, 5));
            
        //    return Color.FromArgb(GetByte(value, 1), GetByte(value, 3), GetByte(value, 5), GetByte(value, 7));
        //}

        public static Color FromString(this Color color, string value)
        {
            if (value.Length < 8)
            {
                color = Color.FromArgb(Convert.ToByte("FF"), GetByte(value, 1), GetByte(value, 3), GetByte(value, 5));
                return Color.FromArgb(Convert.ToByte("FF"), GetByte(value, 1), GetByte(value, 3), GetByte(value, 5));
            }

            color = Color.FromArgb(GetByte(value, 1), GetByte(value, 3), GetByte(value, 5), GetByte(value, 7));
            return Color.FromArgb(GetByte(value, 1), GetByte(value, 3), GetByte(value, 5), GetByte(value, 7));
        }

        private static byte GetByte(string value, int start)
        {
            return Convert.ToByte(value.Substring(start, 2), 16);
        }

        public static string ToARGBString(this Color color)
        {
            string test = color.ToString();
            if (color.ToString().Length > 8)
                return color.ToString();
            return string.Format("{0}{1}", "#", color.ToString().Substring(3));

            //if (value.Length < 9)
            //    return value;

            //return string.Format("{0}{1}", "#", value.Substring(3));
        }

        public static string ToRGBString(this Color color)
        {
            return string.Format("{0}{1}", "#", color.ToString().Substring(3));
        }

        //public static double Opacity(this Color color)
        //{

        //}
    }
}
